package Pages;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import Const.GConfig;
import Core.GLives;
import Core.GMain;
import Core.Slide;

public class SplashScreen extends Slide {
	long iTime = 0;
	int iStep = 0;
	Image imglogo = null;
	Image imgBg = null;
	Image imgNap = null;
	Image imgTen = null;
	
	int h, h1, hf;
	
	public void init(){
		try {
			imglogo = Image.createImage("/jamo.png");
			imgBg = Image.createImage("/bg.png");
			imgNap = Image.createImage("/naphom.png");
			imgTen = Image.createImage("/tengame.png");

			iTime =  System.currentTimeMillis();
			hf = imgBg.getHeight()>>1;
		} catch (IOException e) {}
		
		readAppInfo();
		
		// an hien game khac
		MainMenu.hasGamekhac = true;
		HttpConnection conn = null;
		String url = "http://203.162.70.36/highscore/?ctrl=listgamestore";
		try {
			conn = (HttpConnection) Connector.open(url,Connector.READ);
			conn.setRequestProperty("User-Agent", "J2ME;"+GConfig.MaGame+"-"+GLives.ins.version+ ";" + GLives.ins.Partner); //  J2ME;mã game - version
						
	        if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
	        	InputStream	iStrm;
	        	byte[] data;
	        	
	        	iStrm = conn.openInputStream();
	        	ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
	        				
	        	int ch;
				while ((ch = iStrm.read()) != -1)
					bStrm.write(ch);
				
				data = bStrm.toByteArray();
				
				bStrm.close();
								
				if(new String(data).startsWith("R:")){
					MainMenu.hasGamekhac = false;
				}
	        } else {
				MainMenu.hasGamekhac = false;
            }
		}
	    catch (Exception e){
	    	MainMenu.hasGamekhac = false;
	    }
	}
	
	public void destroy(){
		imglogo = null;
		imgBg = null;
		imgNap = null;
		imgTen = null;
	}
	
	public void update(){
		if(iStep == 0){
			if(System.currentTimeMillis() - iTime > 2000) iStep++;
		} else if(iStep == 1) {
			if(System.currentTimeMillis() - iTime > 2200) iStep++;
		} else if(iStep == 2) {
			if(h < hf) h += (hf - h + 8) >> 3;
			else iStep++;
		} else if(iStep == 3){
			if(h1 < 25) h1 += (25 - h1 + 4) >> 2;
			else {iStep++; iTime =  System.currentTimeMillis();}
		} else if(iStep == 4){
			if(System.currentTimeMillis() - iTime > 700) iStep++;
		} else
			changeState(State.ENABLE_SOUND);
			
	}
	public void paint(Graphics g){
		g.setColor(0);
		g.fillRect(0, 0, GLives.ScreenWidth, GLives.ScreenHeight);
		g.setClip(inX, inY, inW, inH);
		
		if(iStep==0)
			g.drawImage(imglogo, GLives.ScreenWidth >> 1, GLives.ScreenHeight >> 1, Graphics.HCENTER | Graphics.VCENTER);
		else {
			g.drawRegion(imgBg, 0, hf - h, imgBg.getWidth(), h<<1, 0, GLives.ScreenWidth >> 1, GLives.ScreenHeight >> 1, Graphics.HCENTER | Graphics.VCENTER);
			g.drawImage(imgNap, GLives.ScreenWidth>>1, (GLives.ScreenHeight>>1)-h, Graphics.HCENTER|Graphics.BOTTOM);
			g.drawRegion(imgNap, 0, 0, imgNap.getWidth(), imgNap.getHeight(), Sprite.TRANS_MIRROR_ROT180, GLives.ScreenWidth>>1, (GLives.ScreenHeight>>1) + h, Graphics.HCENTER|Graphics.TOP);
			if(iStep >= 3)
				g.drawImage(imgTen, GLives.ScreenWidth>>1, inY - 20 + h1, Graphics.HCENTER|Graphics.TOP);
		} 
	}
	
	public void readAppInfo(){
		try {
			String str = "";
			InputStream is = GMain.gMidlet.getClass().getResourceAsStream("/code.dat");
			while(true){
				int c = is.read();
				if(c == -1)
					break;
				else if (c != '\n' && c != '\r' && c != '\t')
					str += (char)c;
			}
			is.close();
			str = str.trim();
			String[] arr = Static.split(str, "#");

			if(arr.length > 0){
				EndGame.cuphap = arr[0];
				EndGame.dauso = arr[1];
				GLives.ins.Partner = arr[2];
			} 
			
			System.out.println("--- Cu Phap:=<"+EndGame.cuphap+"> Dau So:=<" + EndGame.dauso +"> Partner:=<"+ GLives.ins.Partner +">");
		} catch (IOException e) {
			EndGame.cuphap = "UN";
			e.printStackTrace();
		}
	}
}
